//
//  MBBaseHttpClient.m
//  MBObjectServer
//
//  Created by admin on 3/3/14.
//  Copyright (c) 2014 MBpayer. All rights reserved.
//

#import "MBBaseHttpClient.h"
#import "Headertest.h"
@implementation MBBaseHttpClient

@synthesize delegate;

+(MBBaseHttpClient *)sharedMBBaseHttpClient{

//    NSString *urlStr = @"http://10.100.200.6:8095/MBStoreSrv/api/";
     NSString *urlStr = @"http://180.168.84.166/MBStoreSrv/api/";
//    http://180.168.84.166/MBStoreSrv/api/search.shtml
//      NSString *urlStr =   URL_SEVER ;



    static dispatch_once_t pred;
    static MBBaseHttpClient *_sharedMBBaseHttpClient = nil;
    
    dispatch_once(&pred, ^{ _sharedMBBaseHttpClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:urlStr]]; });
    return _sharedMBBaseHttpClient;
}
-(id)initWithBaseURL:(NSURL *)url{
    self = [super initWithBaseURL:url];
    if (!self) {
        return nil;
    }
    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
    [self setDefaultHeader:@"Accept" value:@"application/json"];
    return self;
}
+(MBBaseHttpClient *)sharedPayHttpClient
{
    
//    NSString *urlStr = @"http://cloudpos.mb.com/pay/";
     NSString *urlStr = @"http://cloudpos.mb.com/handler/";
    static dispatch_once_t pred;
    static MBBaseHttpClient *_sharedMBBaseHttpClient = nil;
    
    dispatch_once(&pred, ^{ _sharedMBBaseHttpClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:urlStr]]; });
    return _sharedMBBaseHttpClient;
}

//直接转化json数据
-(void)getJsonDataWithPath:(NSString *)path withAction:(NSString *)actionName WithParameters:(NSDictionary *)parameters{
    NSURL *url = [NSURL URLWithString:path relativeToURL:self.baseURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        if ([self.delegate respondsToSelector:@selector(MBBaseHttpClient: WithPath: didUpdateWithData:)]) {
            [self.delegate MBBaseHttpClient:self WithPath:actionName didUpdateWithData:JSON];
        }
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        if ([self.delegate respondsToSelector:@selector(MBBaseHttpClient: WithPath: didFailWithError:)]) {
            [self.delegate MBBaseHttpClient:self WithPath:actionName didFailWithError:error];
        }

        NSLog(@"ERROR: %@", error);
    }];
    
    [operation start];
}

//获取数据
-(void)updateDataWithPath:(NSString *)path  withAction:(NSString *)actionName WithParameters:(NSDictionary *)parameters {
    [self getPath:path
       parameters:parameters
          success:^(AFHTTPRequestOperation *operation, id responseObject) {

              if ([self.delegate respondsToSelector:@selector(MBBaseHttpClient: WithPath: didUpdateWithData:)]) {
                  [self.delegate MBBaseHttpClient:self WithPath:actionName didUpdateWithData:responseObject];
              }
              NSLog(@"~~~%@",responseObject);
          }
          failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              if ([self.delegate respondsToSelector:@selector(MBBaseHttpClient: WithPath: didFailWithError:)]) {
                  [self.delegate MBBaseHttpClient:self WithPath:actionName didFailWithError:error];
              }
              NSLog(@"error: %@",error.description);
          }];
    
    
}
//提交数据
-(void)postDataWithPath:(NSString *)path  withAction:(NSString *)actionName WithParameters:(NSDictionary *)parameters {
//    [self setParameterEncoding:AFJSONParameterEncoding];
    [self postPath:path
        parameters:parameters
        success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
            NSLog(@"URL is :%@",[[operation.request URL] absoluteString]);
            NSLog(@"success");
            if ([self.delegate respondsToSelector:@selector(MBBaseHttpClient: WithPath: didUpdateWithData:)])
            {
                [self.delegate MBBaseHttpClient:self WithPath:actionName didUpdateWithData:responseObject];
            }
        }
        failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"error: %@",error.description);
            if ([self.delegate respondsToSelector:@selector(MBBaseHttpClient: WithPath: didFailWithError:)]) {
                [self.delegate MBBaseHttpClient:self WithPath:actionName didFailWithError:error];
            }
        }];
}
-(void)postDataWithPath:(NSString *)path  withAction:(NSString *)actionName WithParameters:(NSDictionary *)parameters block:(void(^)(id json))processResult failblock:(void(^)(NSError *error))failresult{
    
    [self postPath:path
        parameters:parameters
           success:^(AFHTTPRequestOperation *operation, id responseObject) {
               NSLog(@"URL is :%@",[[operation.request URL] absoluteString]);
               processResult(responseObject);
           }
           failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"error: %@",error.description);
               failresult(error);
           }];
    
    
    [self getPath:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
         processResult(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failresult(error);
    }];

}
@end
